package com.itheima;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisTestUser {
    @Test
    public void test01() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> userList = mapper.findAllUsers();

        for (User user : userList) {
            System.out.println(user);
        }

        // 释放资源
        sqlSession.close();
    }

    @Test
    public void test02() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 参数
        int id = 1;
        User user = mapper.selectById(id);
        System.out.println(user);

        // 释放资源
        sqlSession.close();
    }

    @Test
    public void test03() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 参数
        int id = 3;
        mapper.deleteById(id);

        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    @Test
    public void test04() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 参数
        // User user = new User(1, "孙悟空", Date.valueOf("1999-09-09"), "妖", "花果山");

        // 动态SQL
        User user = new User();
        user.setId(1);
        user.setUsername("猴精");
        mapper.update(user);

        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    @Test
    public void test05() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 参数
        User user = new User(0, "白龙马", Date.valueOf("2000-10-10"), "妖", "东海");
        mapper.add(user);
        System.out.println("添加后: " + user);

        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    @Test
    public void test06() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 参数
        String username = "精";
        String sex = "女";

        // 处理参数
        username = "%" + username + "%"; // "%精%"

        User user = new User();
        user.setUsername(username);
        user.setSex(sex);

        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("sex", sex);

        List<User> userList = mapper.selectByCondition(username, sex);
        // List<User> userList = mapper.selectByCondition(user);
        // List<User> userList = mapper.selectByCondition(map);

        // 动态SQL
        // List<User> userList = mapper.selectByCondition(null, null);

        for (User u : userList) {
            System.out.println(u);
        }

        // 释放资源
        sqlSession.close();
    }

    // 通过id删除多个用户
    @Test
    public void test07() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        int[] ids = new int[] {1, 3, 5, 6};
        mapper.deleteByIds(ids);

        sqlSession.commit();
        sqlSession.close();
    }
}
